package com.supersit.greensproject.view;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.supersit.greensproject.R;
import com.supersit.greensproject.utils.T;

public class SyncHorizontalScrollView extends HorizontalScrollView {

    private View view;
    private ImageView leftImage;
    private ImageView rightImage;
    private ImageView iv_nav_indicator;
    private int windowWitdh = 0;
    private Activity mContext;
    private int indicatorWidth;
    private String[] tabTitle;
    private LayoutInflater mInflater;
    private RadioGroup rg_nav_content;
    private int currentIndicatorLeft = 0;
    private ViewPager mViewPager;


    public void setSomeParam(View view, ImageView leftImage,
                             ImageView rightImage, Activity context, String[] tabTitle, ImageView iv_nav_indicator, RadioGroup rg_nav_content, ViewPager mViewPager) {
        this.mContext = context;
        this.view = view;
        this.leftImage = leftImage;
        this.rightImage = rightImage;
        this.rg_nav_content = rg_nav_content;
        this.iv_nav_indicator = iv_nav_indicator;
        this.mViewPager = mViewPager;
        DisplayMetrics dm = new DisplayMetrics();
        this.mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        windowWitdh = dm.widthPixels;
        indicatorWidth = dm.widthPixels / tabTitle.length;
        this.tabTitle = tabTitle;

        ViewGroup.LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
        cursor_Params.width = indicatorWidth;// 初始化滑动下标的宽
        iv_nav_indicator.setLayoutParams(cursor_Params);

        //获取布局填充器
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //另一种方式获取
//		LayoutInflater mInflater = LayoutInflater.from(this);

        initNavigationHSV();

    }

    public SyncHorizontalScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public SyncHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    // 显示和隐藏左右两边的箭头
    public void showAndHideArrow() {
        if (!mContext.isFinishing() && view != null) {
            this.measure(0, 0);
            if (windowWitdh >= this.getMeasuredWidth()) {
                leftImage.setVisibility(View.GONE);
                rightImage.setVisibility(View.GONE);
            } else {
                if (this.getLeft() == 0) {
                    leftImage.setVisibility(View.GONE);
                    rightImage.setVisibility(View.VISIBLE);
                } else if (this.getRight() == this.getMeasuredWidth()
                        - windowWitdh) {
                    leftImage.setVisibility(View.VISIBLE);
                    rightImage.setVisibility(View.GONE);
                } else {
                    leftImage.setVisibility(View.VISIBLE);
                    rightImage.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (!mContext.isFinishing() && view != null && rightImage != null
                && leftImage != null) {
            if (view.getWidth() <= windowWitdh) {
                leftImage.setVisibility(View.GONE);
                rightImage.setVisibility(View.GONE);
            } else {
                if (l == 0) {
                    leftImage.setVisibility(View.GONE);
                    rightImage.setVisibility(View.VISIBLE);
                } else if (view.getWidth() - l == windowWitdh) {
                    leftImage.setVisibility(View.VISIBLE);
                    rightImage.setVisibility(View.GONE);
                } else {
                    leftImage.setVisibility(View.VISIBLE);
                    rightImage.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private void initNavigationHSV() {

        rg_nav_content.removeAllViews();

        for (int i = 0; i < tabTitle.length; i++) {

            RadioButton rb = (RadioButton) mInflater.inflate(R.layout.nav_radiogroup_item, null);
            rb.setId(i);
            rb.setText(tabTitle[i]);
            rb.setLayoutParams(new ViewGroup.LayoutParams(indicatorWidth,
                    ViewGroup.LayoutParams.MATCH_PARENT));

            rg_nav_content.addView(rb);
        }
        rg_nav_content.setOnCheckedChangeListener(onCheckedChangeListener);
        mViewPager.addOnPageChangeListener(onPageChangeListener);
        showAndHideArrow();
        ((RadioButton) rg_nav_content.getChildAt(0)).setChecked(true);
    }

    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener()

    {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (rg_nav_content.getChildAt(checkedId) != null) {
                TranslateAnimation animation = new TranslateAnimation(
                        currentIndicatorLeft,
                        ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft(), 0f, 0f);
                animation.setInterpolator(new LinearInterpolator());
                animation.setDuration(100);
                animation.setFillAfter(true);

                //执行位移动画
                iv_nav_indicator.startAnimation(animation);

                mViewPager.setCurrentItem(checkedId);    //ViewPager 跟随一起 切换

                //记录当前 下标的距最左侧的 距离
                currentIndicatorLeft = ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft();
                //T.showShort(mContext, checkedId + "");
//                mHsv.smoothScrollTo(
//                        (checkedId > icv1 ? ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg_nav_content.getChildAt(icv2)).getLeft(), 0);
                smoothScrollBy(0, 0);
            }

        }
    };
    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //T.showShort(context,position+"");
            if (rg_nav_content != null && rg_nav_content.getChildCount() > position) {
                ((RadioButton) rg_nav_content.getChildAt(position)).performClick();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
}
